//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.mercury.emqx.config;

import com.github.lianjiatech.retrofit.spring.boot.annotation.RetrofitScan;
import com.mercury.emqx.client.MqttClient;
import com.mercury.emqx.manager.MqttManager;
import com.mercury.emqx.properties.MqttProperties;
import org.apache.commons.lang3.RandomStringUtils;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.messaging.MessageChannel;

import javax.annotation.Resource;


/**
 * @author mercury
 * mqtt配置
 */
@Configuration
@EnableConfigurationProperties({MqttProperties.class})
@RetrofitScan("spring.mqtt.client")
public class MqttAutoConfiguration {
    private static final Logger log = LoggerFactory.getLogger(MqttAutoConfiguration.class);
    @Resource
    private MqttClient mqttClient;

    public MqttAutoConfiguration() {
    }

    @Bean
    @ConditionalOnProperty(
            name = {"spring.mqtt.enabled"},
            havingValue = "true"
    )
    public MqttProperties mqttProperties(MqttProperties mqttProperties) {
        String clientId = mqttProperties.getUsername() + "-" + RandomStringUtils.randomAlphanumeric(6);
        String password = RandomStringUtils.randomAlphanumeric(10);
        mqttProperties.setClientId(clientId);
        mqttProperties.setPassword(password);
        return mqttProperties;
    }

    @Bean
    @ConditionalOnMissingBean({MqttConnectOptions.class})
    @ConditionalOnProperty(
            name = {"spring.mqtt.enabled"},
            havingValue = "true"
    )
    public MqttConnectOptions mqttConnectOptions(MqttProperties mqttProperties) {
        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setUserName(mqttProperties.getUsername());
        mqttConnectOptions.setPassword(mqttProperties.getPassword().toCharArray());
        mqttConnectOptions.setConnectionTimeout(mqttProperties.getConnectTimeout());
        mqttConnectOptions.setServerURIs(mqttProperties.getHosts());
        return mqttConnectOptions;
    }

    @Bean
    @ConditionalOnMissingBean({MqttPahoClientFactory.class})
    @ConditionalOnProperty(
            name = {"spring.mqtt.enabled"},
            havingValue = "true"
    )
    public MqttPahoClientFactory mqttPahoClientFactory(MqttProperties mqttProperties) {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        factory.setConnectionOptions(this.mqttConnectOptions(mqttProperties));
        return factory;
    }

    @Bean
    @ConditionalOnMissingBean({MessageChannel.class})
    @ConditionalOnProperty(
            name = {"spring.mqtt.enabled"},
            havingValue = "true"
    )
    public MessageChannel inputMqttChannel() {
        return new DirectChannel();
    }





}
